package com.dianping.cat.consumer.matrix.model.transform;

import com.dianping.cat.consumer.matrix.model.IVisitor;
import com.dianping.cat.consumer.matrix.model.entity.Matrix;
import com.dianping.cat.consumer.matrix.model.entity.MatrixReport;
import com.dianping.cat.consumer.matrix.model.entity.Ratio;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;


public class DefaultNativeBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private DataOutputStream m_out;

  public DefaultNativeBuilder(OutputStream out) { this(out, null); }


  public DefaultNativeBuilder(OutputStream out, IVisitor visitor) {
    this.m_out = new DataOutputStream(out);
    this.m_visitor = (visitor == null) ? this : visitor;
  }

  public static byte[] build(MatrixReport matrixReport) {
    ByteArrayOutputStream out = new ByteArrayOutputStream(8192);

    build(matrixReport, out);
    return out.toByteArray();
  }


  public static void build(MatrixReport matrixReport, OutputStream out) { matrixReport.accept(new DefaultNativeBuilder(out)); }



  public void visitMatrix(Matrix matrix) {
/*  41 */     if (matrix.getType() != null) {
/*  42 */       writeTag(1, 1);
/*  43 */       writeString(matrix.getType());
    }

/*  46 */     if (matrix.getName() != null) {
/*  47 */       writeTag(2, 1);
/*  48 */       writeString(matrix.getName());
    }

/*  51 */     writeTag(3, 0);
/*  52 */     writeInt(matrix.getCount());

/*  54 */     writeTag(4, 0);
/*  55 */     writeLong(matrix.getTotalTime());

/*  57 */     if (matrix.getUrl() != null) {
/*  58 */       writeTag(5, 1);
/*  59 */       writeString(matrix.getUrl());
    }

/*  62 */     if (!matrix.getRatios().isEmpty()) {
/*  63 */       writeTag(33, 2);
/*  64 */       writeInt(matrix.getRatios().size());

/*  66 */       for (Ratio ratio : matrix.getRatios().values()) {
/*  67 */         ratio.accept(this.m_visitor);
      }
    }

/*  71 */     writeTag(63, 3);
  }


  public void visitMatrixReport(MatrixReport matrixReport) {
/*  76 */     writeTag(63, 0);

    if (matrixReport.getDomain() != null) {
/*  79 */       writeTag(1, 1);
/*  80 */       writeString(matrixReport.getDomain());
    }

/*  83 */     if (matrixReport.getStartTime() != null) {
/*  84 */       writeTag(2, 1);
/*  85 */       writeDate(matrixReport.getStartTime());
    }

/*  88 */     if (matrixReport.getEndTime() != null) {
/*  89 */       writeTag(3, 1);
/*  90 */       writeDate(matrixReport.getEndTime());
    }

/*  93 */     if (matrixReport.getDomainNames() != null) {
/*  94 */       writeTag(4, 2);
/*  95 */       writeInt(matrixReport.getDomainNames().size());

/*  97 */       for (String domain : matrixReport.getDomainNames()) {
/*  98 */         writeString(domain);
      }
    }

/* 102 */     if (!matrixReport.getMatrixs().isEmpty()) {
/* 103 */       writeTag(33, 2);
/* 104 */       writeInt(matrixReport.getMatrixs().size());

/* 106 */       for (Matrix matrix : matrixReport.getMatrixs().values()) {
/* 107 */         matrix.accept(this.m_visitor);
      }
    }

/* 111 */     writeTag(63, 3);
  }


  public void visitRatio(Ratio ratio) {
/* 116 */     if (ratio.getType() != null) {
/* 117 */       writeTag(1, 1);
/* 118 */       writeString(ratio.getType());
    }

/* 121 */     writeTag(2, 0);
/* 122 */     writeInt(ratio.getMin());

/* 124 */     writeTag(3, 0);
/* 125 */     writeInt(ratio.getMax());

/* 127 */     writeTag(4, 0);
/* 128 */     writeInt(ratio.getTotalCount());

/* 130 */     writeTag(5, 0);
/* 131 */     writeLong(ratio.getTotalTime());

/* 133 */     if (ratio.getUrl() != null) {
/* 134 */       writeTag(6, 1);
/* 135 */       writeString(ratio.getUrl());
    }

/* 138 */     writeTag(63, 3);
  }

  private void writeDate(Date value) {
    try {
/* 143 */       writeVarint(value.getTime());
/* 144 */     } catch (Exception e) {
/* 145 */       throw new RuntimeException(e);
    }
  }

  private void writeInt(int value) {
    try {
/* 151 */       writeVarint(value & 0xFFFFFFFFL);
/* 152 */     } catch (Exception e) {
/* 153 */       throw new RuntimeException(e);
    }
  }

  private void writeLong(long value) {
    try {
/* 159 */       writeVarint(value);
/* 160 */     } catch (Exception e) {
/* 161 */       throw new RuntimeException(e);
    }
  }

  private void writeString(String value) {
    try {
/* 167 */       this.m_out.writeUTF(value);
/* 168 */     } catch (IOException e) {
/* 169 */       throw new RuntimeException(e);
    }
  }

  private void writeTag(int field, int type) {
    try {
/* 175 */       this.m_out.writeByte((field << 2) + type);
/* 176 */     } catch (IOException e) {
/* 177 */       throw new RuntimeException(e);
    }
  }

  protected void writeVarint(long value) throws IOException {
    while (true) {
/* 183 */       if ((value & 0xFFFFFFFFFFFFFF80L) == 0L) {
/* 184 */         this.m_out.writeByte((byte)(int)value);
        return;
      }
/* 187 */       this.m_out.writeByte((byte)(int)value & 0x7F | 0x80);
/* 188 */       value >>>= 7;
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-consumer-3.0.0.jar!/com/dianping/cat/consumer/matrix/model/transform/DefaultNativeBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */